package com.thinkgem.jeesite.modules.fee.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.building.entity.Building;
import com.thinkgem.jeesite.modules.building.entity.Room;
import com.thinkgem.jeesite.modules.building.service.BuildingService;
import com.thinkgem.jeesite.modules.building.service.RoomService;
import com.thinkgem.jeesite.modules.fee.entity.Fee;
import com.thinkgem.jeesite.modules.fee.service.FeeService;
import com.thinkgem.jeesite.modules.record.entity.Record;
import com.thinkgem.jeesite.modules.record.service.RecordService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.unfee.entity.Unfee;
import com.thinkgem.jeesite.modules.unfee.service.UnfeeService;

@Controller
@RequestMapping(value="${adminPath}/fee")
public class FeeController extends BaseController{

	@Autowired
	FeeService feeservice;
	@Autowired
	SystemService systemservice;
	@Autowired
	BuildingService buildingservice;
	@Autowired
	RoomService roomservice;
	@Autowired
	UnfeeService unfeeservice;
	@Autowired
	RecordService recordservice;
	/**
	 * 费率列表
	 * */
	@RequestMapping(value="/feelist")
	public String feelist(HttpServletRequest request,HttpServletResponse response,Model model){
		
		Page<Fee> page = feeservice.findPage(new Page<Fee>(request,response),new Fee());
		List<Fee> result = new ArrayList<Fee>();
		for(int i=0;i<page.getList().size();i++){
			Building building = buildingservice.get(page.getList().get(i).getBuilding().getId());
			String[] roomIds = page.getList().get(i).getRoomIds().split(",");
			for(int n=0;n<roomIds.length;n++){
				Fee fee = new Fee();
				Room room = roomservice.get(roomIds[n]);
				if(room ==null) continue;
				User user = systemservice.getUser(room.getUser().getId());
				fee.setBuilding(building);
				fee.setRoom(room);
				fee.setUser(user);
				fee.setPrice(page.getList().get(i).getPrice());
				fee.setId(page.getList().get(i).getId());
				result.add(fee);
			}
			
		}
//		for(int i=0;i<result.size();i++){
//			System.out.println(result.get(i).getUser().getName());
//		}
		page.setList(result);
		page.setCount(result.size());
		model.addAttribute("page", page);
		return "modules/fee/feeList";
		
	}
	/**
	 * 跳转到对应页面
	 * */
	@RequestMapping(value="/addFee")
	public String addFee(Model model){
		model.addAttribute("buildingName", buildingservice.findAllBuildingName());
		model.addAttribute("fee", new Fee());
		return "modules/fee/feeForm";
	}
	/**
	 * 保存按钮
	 * */
	@RequestMapping(value="/save")
	public String feeForm(Fee fee,Model model){
		if(fee.getPrice().length() == 0 || fee.getRoomIds().length() ==0){
			model.addAttribute("buildingName", buildingservice.findAllBuildingName());
			model.addAttribute("fee", fee);
			return "modules/fee/feeForm";
		}
		fee.setId(UUID.randomUUID().toString());
		feeservice.insert(fee);
		return "redirect:"+Global.getAdminPath()+"/fee/feelist";
	}
	/**
	 * 删除物业费，其实就是从拼接中去除再更新
	 * */
	@RequestMapping(value="deleteFee")
	public String deleteFee(@RequestParam("feeId")String feeId,@RequestParam("roomId")String roomId,Model model){
		Fee fee = feeservice.get(feeId);
		String[] roomIds = fee.getRoomIds().split(",");
		String temp = "";
		for(String s:roomIds){
			if(!s.equals(roomId)) temp+=s+",";
		}
		if(temp.length() > 10){
			fee.setRoomIds(temp);
			feeservice.undateRoomIds(fee);
		}
		return "redirect:"+Global.getAdminPath()+"/fee/feelist";
	}
	@RequestMapping(value="/roomList")
	public String roomList(HttpServletRequest request,HttpServletResponse response,Model model){
		model.addAttribute("teacherids", "");
		Page<Room> page = roomservice.findMyAdminRoom(new Page<Room>(request,response) ,new Room());
		model.addAttribute("page", page);
		return "modules/fee/roomList";
	}
	@RequestMapping(value="/feeMonth")
	public String feeMonth(){
		return "modules/fee/feeMonth";
	}
	/**
	 * 月末扣费
	 * */
	@RequestMapping(value="doFeeMonth")
	public String doFeeMonth(HttpServletRequest request,HttpServletResponse response,Model model){
		Date now  = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Page<Fee> feepage = feeservice.findPage(new Page<Fee>(request,response),new Fee());
		Page<Room> roompage = roomservice.findPage(new Page<Room>(request,response) ,new Room());
		for(int i=0;i<feepage.getList().size();i++){
			String[] roomlist = feepage.getList().get(i).getRoomIds().split(",");
			for(int n = 0;n<roomlist.length;n++){
				for(int j=0;j<roompage.getList().size();j++){
					if(roompage.getList().get(j).getId().equals(roomlist[n])){
						User user = systemservice.getUser(roompage.getList().get(j).getUser().getId());
						if(Double.parseDouble(user.getBalance()) >= Double.parseDouble(feepage.getList().get(i).getPrice())){
							user.setBalance(Double.parseDouble(user.getBalance()) - Double.parseDouble(feepage.getList().get(i).getPrice())+"");
							systemservice.saveUser(user);
							/*添加一条记录到记录表中*/
							Record record = new Record();
							record.setId(UUID.randomUUID().toString());
							record.setCreateTime(format.format(now).toString());
							record.setMoney(feepage.getList().get(i).getPrice());
							record.setUser(UserUtils.getUser());
							record.setRoom(roompage.getList().get(j));
							recordservice.insertRecord(record);
							
						}
						//否则  加入到欠费表中
						else{
							Unfee unfee = new Unfee();
							unfee.setId(UUID.randomUUID().toString());
							unfee.setCreateTime(format.format(now).toString());
							unfee.setPrice(feepage.getList().get(i).getPrice());
							unfee.setUser(user);
							unfeeservice.insert(unfee);
							
						}
					}
				}
			}
		}
		return "redirect:"+Global.getAdminPath()+"/fee/feelist";
	}
}
